-module(gtp_bearer_server_sup).
-behaviour(supervisor).
-export([init/1]).
-export([start_link/0,start_child/1]).

-export([forward/2]).
-define(SERVER,?MODULE).

forward(Bearer_Server_IP,Packet_Tuple) ->
	Pid = case get(Bearer_Server_IP) of
		  undefined ->
			{ok,Child_Id} = start_child(Bearer_Server_IP),
			put(Bearer_Server_IP,Child_Id),
			error_logger:info_msg("a bearer server is start ~p",[Child_Id]),
			Child_Id;
		  Res ->
			Res
		  end,
	gtp_bearer_server:handle(Pid,{packet_comming,Packet_Tuple}).

start_link() ->
	supervisor:start_link({local,?SERVER},?MODULE,[]).
init([]) ->
	Children = [
		{gtp_bearer_server,{gtp_bearer_server,start_link,[]},temporary,brutal_kill,worker,[gtp_bearer_server]}
		],
	RestartStrategy = {simple_one_for_one,0,1},
	{ok,{RestartStrategy,Children}}.
start_child(IP) ->
	supervisor:start_child(?SERVER,[IP]).
